document.addEventListener('DOMContentLoaded', () => {
    document.getElementById('bookmarkBtn').addEventListener('click', async () => {
        console.log('按钮被点击');
        const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
        console.log('tab',tab);
        if (!tab) {
            console.error('没有获取到当前活动标签');
            return;
        }

        chrome.storage.local.get('config', async (data) => {
            console.log('data',data);
            const apiUrl = data.config.apiUrl;
            const username = data.config.username;
            const password = data.config.password;
            console.log('apiUrl',apiUrl);
            const base64Credentials = btoa(`${username}:${password}`);


        
            
            // 准备请求数据
            const payload = {
                url: tab.url,
                title: tab.title,
                summary: tab.summary,
       
            };

            // 发起请求
            try {
                const response = await fetch(apiUrl, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'Authorization': `Basic ${base64Credentials}`
                    },
                    body: JSON.stringify(payload)
                });

                if (response.ok) {
                    const result = await response.json();
                    console.log('请求成功:', result);
                } else {
                    console.error('请求失败:', response.statusText);
                }
            } catch (error) {
                console.error('请求错误:', error);
            }
        });
    
    });
});
// 定义 showModal 函数
function showModal(message) {
    const modal = document.createElement('div');
    modal.style.position = 'fixed';
    modal.style.top = '50%';
    modal.style.left = '50%';
    modal.style.transform = 'translate(-50%, -50%)';
    modal.style.padding = '20px';
    modal.style.backgroundColor = 'white';
    modal.style.boxShadow = '0 0 10px rgba(0, 0, 0, 0.5)';
    modal.style.zIndex = '1000';

    const messageText = document.createElement('p');
    messageText.textContent = message;
    modal.appendChild(messageText);

    const closeButton = document.createElement('button');
    closeButton.textContent = '关闭';
    closeButton.onclick = () => {
        document.body.removeChild(modal);
    };
    modal.appendChild(closeButton);

    document.body.appendChild(modal);
}